from scapy.all import *
from tkinter import *
import gmssl.sm3 as sm3
warnings.filterwarnings('ignore')

def chk_sum(a):
    b = str(a)
    length=len(b)
    if len(b) % 2 != 0:
        b += "0"
    ans_list = []
    for i in range(int(len(b) / 2)):
        temp = b[i * 2:i * 2 + 2].encode('utf-8')
        ans_list.append(sm3.sm3_kdf(temp, 1))
    ans = ""
    for i in ans_list:
        for j in range(2):
            temp2=str(i[j])

            temp_list = ['a','b','c','d','e','f']
            if temp2 in temp_list:
                temp2=str(temp_list.index(temp2))
            ans += temp2

    end = ""
    for i in range(length):
        end += ans[len(ans) - 1] + ans[0] + ans[int(len(ans) / 3)] + ans[int(len(ans) / 2)]
    return ans


def  check_pkt(packet):
    master = Tk()
    master.title('密钥')
    if chk_sum(packet['TCP'].sport) == str(packet["TCP"].ack):
        frame1 = Frame(master)
        frame1.pack(fill=X)
        label1 = Label(frame1, text='传输出现问题，请重传！')
        label1.grid(row=1, column=0)
        master.mainloop()
        exit(0)

packet = sniff(iface='Intel(R) Wireless-AC 9560 160MHz', prn=check_pkt, lfilter=lambda p: "GET" in str(p),
                       filter="tcp port 80")

